package net.pojoin;

import net.pojoin.listener.GeneralListener;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class CalculatorActivity extends Activity {
	
	private String result = "";
	
	private EditText resultEditText = null;
	
	private Button num1Button = null;
	private Button num2Button = null;
	private Button num3Button = null;
	private Button num4Button = null;
	private Button num5Button = null;
	private Button num6Button = null;
	private Button num7Button = null;
	private Button num8Button = null;
	private Button num9Button = null;
	private Button num0Button = null;
	
	private Button pointButton = null;
	private Button delButton = null;
	private Button equalButton = null;
	
	private Button addButton = null;
	private Button subButton = null;
	private Button mulButton = null;
	private Button divButton = null;
	
	public String getResult(){
		this.result = this.resultEditText.getText().toString();
		return this.result;
	}
	public void setResult(String result){
		this.result = result;
	}
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        this.resultEditText = (EditText) this.findViewById(R.id.result);
        
        this.num0Button = (Button) this.findViewById(R.id.num0);
        this.num1Button = (Button) this.findViewById(R.id.num1);
        this.num2Button = (Button) this.findViewById(R.id.num2);
        this.num3Button = (Button) this.findViewById(R.id.num3);
        this.num4Button = (Button) this.findViewById(R.id.num4);
        this.num5Button = (Button) this.findViewById(R.id.num5);
        this.num6Button = (Button) this.findViewById(R.id.num6);
        this.num7Button = (Button) this.findViewById(R.id.num7);
        this.num8Button = (Button) this.findViewById(R.id.num8);
        this.num9Button = (Button) this.findViewById(R.id.num9);
        
        this.pointButton = (Button) this.findViewById(R.id.point);
        this.equalButton = (Button) this.findViewById(R.id.equal);
        this.delButton = (Button) this.findViewById(R.id.del);
        
        this.addButton = (Button) this.findViewById(R.id.add);
        this.subButton = (Button) this.findViewById(R.id.sub);
        this.mulButton = (Button) this.findViewById(R.id.mul);
        this.divButton = (Button) this.findViewById(R.id.div);
        
        this.num0Button.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "0");
				super.resetSelection(resEditText);
			}
        	
		});
        this.num1Button.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "1");
				super.resetSelection(resEditText);
			}
        	
		});
        this.num2Button.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "2");
				super.resetSelection(resEditText);
			}
        	
		});
        this.num3Button.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "3");
				super.resetSelection(resEditText);
			}
        	
		});
        this.num4Button.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "4");
				super.resetSelection(resEditText);
			}
        	
		});
        
        this.num5Button.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "5");
				super.resetSelection(resEditText);
			}
        	
		});
        this.num6Button.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "6");
				super.resetSelection(resEditText);
			}
        	
		});
        this.num7Button.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "7");
				super.resetSelection(resEditText);
			}
        	
		});
        this.num8Button.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "8");
				super.resetSelection(resEditText);
			}
        	
		});
        this.num9Button.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "9");
				super.resetSelection(resEditText);
			}
        	
		});
        
        this.pointButton.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + ".");
				super.resetSelection(resEditText);
			}
        	
		});
        
        this.addButton.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "＋");
				super.resetSelection(resEditText);
			}
        	
		});
        
        this.subButton.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "-");
				super.resetSelection(resEditText);
			}
        	
		});
        this.mulButton.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "×");
				super.resetSelection(resEditText);
			}
        	
		});
        
        this.divButton.setOnClickListener(new GeneralListener(this) {

			@Override
			public void onClick(View v) {
				super.onClick(v);
				EditText resEditText = super.getCalculatorActivity().resultEditText;
				resEditText.setText(super.getCalculatorActivity().getResult() + "÷");
				super.resetSelection(resEditText);
			}
        	
		});
        
    }
}